package com.dtw.ditouwang.ui.home;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.transition.Transition;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Option;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.HomeBean;
import com.dtw.ditouwang.databinding.FragmentHomeBinding;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.WebActivity;
import com.dtw.ditouwang.ui.mine.MyCollectionActivity;
import com.dtw.ditouwang.ui.mine.UserInfoActivity;
import com.dtw.ditouwang.ui.more.EditorChooseNormalActivity;
import com.dtw.ditouwang.ui.more.EditorChooseNormalDetailActivity;
import com.dtw.ditouwang.ui.more.EditorChoosePhotoActivity;
import com.dtw.ditouwang.ui.more.EditorChoosePhotoDetailActivity;
import com.dtw.ditouwang.views.GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeFragment extends BaseFragment implements View.OnClickListener, OnItemClickListener {
    FragmentHomeBinding binding;
    ImagePeopleAdapter imagePeopleAdapterFirst;
    ImagePeopleAdapter imagePeopleAdapterSecond;
    List<String> imgListFirst = new ArrayList<>(), imgListSecond = new ArrayList<>();
    private ImageView imgAdFirst;
    private CircleImageView imgHeadFrist;
    private RecyclerView recyclerFirst;
    private ImageView imgImgFrist;
    private ImageView imgImgSecond;
    private ImageView imgAdSecond;
    private CircleImageView imgHeadSecond;
    private RecyclerView recyclerSecond;
    private TextView textFirst, textSecond;
    HomeBean homeBean;
    RequestOptions requestOptions = RequestOptions.bitmapTransform(new GlideRoundTransform(10));

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(
                inflater, R.layout.fragment_home, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        imgAdFirst = view.findViewById(R.id.img_ad_first);
        imgAdSecond = view.findViewById(R.id.img_ad_second);
        imgHeadFrist = view.findViewById(R.id.img_head_frist);
        imgHeadSecond = view.findViewById(R.id.img_head_second);
        imgHeadFrist.setOnClickListener(this);
        imgHeadSecond.setOnClickListener(this);
        imgImgFrist = view.findViewById(R.id.img_img_frist);
        imgImgSecond = view.findViewById(R.id.img_img_second);
        recyclerFirst = view.findViewById(R.id.recycler_first);
        recyclerSecond = view.findViewById(R.id.recycler_second);
        textFirst = view.findViewById(R.id.text_sys_first);
        textSecond = view.findViewById(R.id.text_sys_second);
        imagePeopleAdapterFirst = new ImagePeopleAdapter(imgListFirst);
        imagePeopleAdapterSecond = new ImagePeopleAdapter(imgListSecond);
        recyclerFirst.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerSecond.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerFirst.setNestedScrollingEnabled(false);
        recyclerFirst.setAdapter(imagePeopleAdapterFirst);
        recyclerSecond.setAdapter(imagePeopleAdapterSecond);
        imagePeopleAdapterFirst.setOnItemClickListener(this);
        imagePeopleAdapterSecond.setOnItemClickListener(this);
        textFirst.setOnClickListener(this);
        textSecond.setOnClickListener(this);
        binding.userheadImageview.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        webApi.getHomeData(UserRepository.getInstance().getUserID()).enqueue(new Callback<HomeBean>() {
            @Override
            public void onResponse(Call<HomeBean> call, Response<HomeBean> response) {
                if (response.body().isResult()) {
                    homeBean = response.body();
                    //here data must be an instance of the class MarsDataProvider
                    HomeBean.DataBean homeBean = response.body().getData();
                    binding.setHome(homeBean);
                    for (int i = 0; i < homeBean.getUser_act().size(); i++) {
                        if (i == 0) {
                            if (homeBean.getUser_act().get(i).getHead() != null) {
                                Glide.with(imgHeadFrist).load(WebApi.BaseUrl + homeBean.getUser_act().get(i).getHead().replace("\\", "/")).into(imgHeadFrist);
                            }
//                            if ("1".equals(homeBean.getUser_act().get(i).getIs_collection())) {
//                                likeFirst.setSelected(true);
//                            }
                            imgListFirst.clear();
                            imgListFirst.addAll(homeBean.getUser_act().get(i).getImg());
                            imagePeopleAdapterFirst.notifyDataSetChanged();

                        } else if (i == 1) {
                            if (homeBean.getUser_act().get(i).getHead() != null) {
                                Glide.with(imgHeadSecond).load(WebApi.BaseUrl + homeBean.getUser_act().get(i).getHead().replace("\\", "/")).into(imgHeadSecond);
                            }
//                            if ("1".equals(homeBean.getUser_act().get(i).getIs_collection())) {
//                                likeSecond.setSelected(true);
//                            }
                            imgListSecond.clear();
                            imgListSecond.addAll(homeBean.getUser_act().get(i).getImg());
                            imagePeopleAdapterSecond.notifyDataSetChanged();
                        }
                    }
                    for (int i = 0; i < homeBean.getAd().size(); i++) {
                        if (homeBean.getAd().get(i).getBackground() != null) {
                            if (i == 0) {
                                Glide.with(imgAdFirst).load(WebApi.BaseUrl + homeBean.getAd().get(i).getBackground().replace("\\", "/"))
                                        .apply(requestOptions).into(imgAdFirst);
                                imgAdFirst.setOnClickListener(HomeFragment.this);
                            } else if (i == 1) {
                                Glide.with(imgAdSecond).load(WebApi.BaseUrl + homeBean.getAd().get(i).getBackground().replace("\\", "/"))
                                        .apply(requestOptions).into(imgAdSecond);
                                imgAdSecond.setOnClickListener(HomeFragment.this);
                            }
                        }
                    }
                    for (int i = 0; i < homeBean.getSys_act().size(); i++) {
                        if (homeBean.getSys_act().get(i).getCover() != null) {
                            if (i == 0) {
                                Glide.with(imgImgFrist).load(WebApi.BaseUrl + homeBean.getSys_act().get(i).getCover().replace("\\", "/")).apply(requestOptions).into(imgImgFrist);
                                imgImgFrist.setOnClickListener(HomeFragment.this);
                            } else if (i == 1) {
                                Glide.with(imgImgSecond).load(WebApi.BaseUrl + homeBean.getSys_act().get(i).getCover().replace("\\", "/")).apply(requestOptions).into(imgImgSecond);
                                imgImgSecond.setOnClickListener(HomeFragment.this);
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<HomeBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (homeBean != null) {
            switch (view.getId()) {
                case R.id.img_ad_first:
                    Intent ad1 = new Intent(getContext(), WebActivity.class);
                    ad1.putExtra(WebActivity.KeyUrl, homeBean.getData().getAd().get(0).getLink());
                    startActivity(ad1);
                    break;
                case R.id.img_ad_second:
                    Intent ad2 = new Intent(getContext(), WebActivity.class);
                    ad2.putExtra(WebActivity.KeyUrl, homeBean.getData().getAd().get(1).getLink());
                    startActivity(ad2);
                    break;
                case R.id.text_sys_first:
                case R.id.img_img_frist:
                    Intent sys1 = new Intent(getContext(), EditorChooseNormalDetailActivity.class);
                    sys1.putExtra(EditorChooseNormalDetailActivity.KeySYSID, homeBean.getData().getSys_act().get(0).getId());
                    startActivity(sys1);
                    break;
                case R.id.text_sys_second:
                case R.id.img_img_second:
                    Intent sys2 = new Intent(getContext(), EditorChooseNormalDetailActivity.class);
                    sys2.putExtra(EditorChooseNormalDetailActivity.KeySYSID, homeBean.getData().getSys_act().get(1).getId());
                    startActivity(sys2);
                    break;
                case R.id.img_head_frist:
                    MyCollectionActivity.start(getContext(),homeBean.getData().getUser_act().get(0).getUser_id());
                    break;
                case R.id.img_head_second:
                    MyCollectionActivity.start(getContext(),homeBean.getData().getUser_act().get(1).getUser_id());
                    break;
                case R.id.userheadImageview:
                    MyCollectionActivity.start(getContext(),UserRepository.getInstance().getUserID());
                    break;
            }
        }
    }

    @Override
    public void onItemClick(int resID, int position, int state) {
        Intent intent = new Intent(getContext(), EditorChoosePhotoDetailActivity.class);
        switch (resID){
            case R.id.recycler_first:
                intent.putExtra(EditorChoosePhotoDetailActivity.KeyID, homeBean.getData().getUser_act().get(0).getId());
                startActivity(intent);
                break;
            case R.id.recycler_second:
                intent.putExtra(EditorChoosePhotoDetailActivity.KeyID, homeBean.getData().getUser_act().get(1).getId());
                startActivity(intent);
                break;
        }
    }
}
